/* Miscellaneous functions. */

#include "defs.h"
#include "data.h"
#include "decl.h"

/*
 * Ensure that the current token is t,
 * and fetch the next token. Otherwise throw an error.
 */
void match(int tk, char *expected_str)
{
    if (Token.token == tk) {
        scan(&Token);
    } else {
        fatals("Expected", expected_str);
    }
}

/*
 * Match a semicon and fetch the next token.
 */
void semi(void) { match(T_SEMI, ";"); }

/*
 * Match a identifier and fetch the next token.
 */
void ident(void) { match(T_IDENT, "identifier"); }

/*
 * Match a left curly bracket and fetch the next token.
 */
void lbrace(void) { match(T_LBRACE, "{"); }

/*
 * Match a right curly bracket and fetch the next token.
 */
void rbrace(void) { match(T_RBRACE, "}"); }

/*
 * Match a left parenthesis and fetch the next token.
 */
void lparen(void) { match(T_LPAREN, "("); }

/*
 * Match a right parenthesis and fetch the next token.
 */
void rparen(void) { match(T_RPAREN, ")"); }

/*
 * Print out fatal messages.
 */
void fatal(char *s)
{
    fprintf(stderr, "%s on line %d\n", s, Line);
    exit(1);
}

void fatals(char *s1, char *s2)
{
    fprintf(stderr, "%s:%s on line %d\n", s1, s2, Line);
    exit(1);
}

void fatald(char *s, int d)
{
    fprintf(stderr, "%s:%d on line %d\n", s, d, Line);
    exit(1);
}

void fatalc(char *s, int c)
{
    fprintf(stderr, "%s:%c on line %d\n", s, c, Line);
    exit(1);
}
